package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import DAO.FavouriesDAO;
import DAO.UserAccountDAO;
import DTO.UserAccountDTO;
import VMO.FavouriesMenuVMO;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class UserServlet
 */
public class UserServlet extends HttpServlet {
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		PrintWriter out = response.getWriter();
		if (action.equals("login")) {
			String username = request.getParameter("username");
			String password = request.getParameter("password");

			UserAccountDAO dao = new UserAccountDAO();
			if (dao.checkAuthenticate(username, password)) {
				HttpSession session = request.getSession();
				session.setAttribute("USER_USERNAME", username);
				response.sendRedirect("home.jsp");
			} else {
				request.setAttribute("ERROR",
						"Incorrect username and password!");
				RequestDispatcher rd = request
						.getRequestDispatcher("user-login.jsp");
				rd.forward(request, response);
			}
		} else if (action.equals("logout")) {
			HttpSession session = request.getSession();
			session.removeAttribute("USER_USERNAME");
			response.sendRedirect("home.jsp");
		} else if (action.equals("register")) {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			String email = request.getParameter("email");

			UserAccountDAO dao = new UserAccountDAO();
			if (dao.checkUsernameExist(username)) {
				request.setAttribute("ERROR",
						"Username has exist! Please choose another!");
				RequestDispatcher rd = request
						.getRequestDispatcher("register.jsp");
				rd.forward(request, response);
			} else if (dao.checkEmailExist(email)) {
				request.setAttribute("ERROR",
						"Email has exist! Please choose another!");
				RequestDispatcher rd = request
						.getRequestDispatcher("register.jsp");
				rd.forward(request, response);
			} else {
				UserAccountDTO user = new UserAccountDTO(username, password,
						email);
				dao.add(user);
			}
		} else if (action.equals("addFavouries")) {
			HttpSession session = request.getSession();
			String username = (String) session.getAttribute("USER_USERNAME");
			int menuId = Integer.parseInt(request.getParameter("menuId"));

			FavouriesDAO dao = new FavouriesDAO();
			dao.add(username, menuId, false);
			out.print("success");
		} else if (action.equals("startMenu")) {
			HttpSession session = request.getSession();
			String username = (String) session.getAttribute("USER_USERNAME");
			int menuId = Integer.parseInt(request.getParameter("menuId"));
			FavouriesDAO dao = new FavouriesDAO();
			if (dao.checkExist(username, menuId)) {
				dao.update(username, menuId);
			} else {
				dao.add(username, menuId, true);
			}
			out.print("success");
		} else if (action.equals("loadFavouries")) {
			if (request.getSession().getAttribute("USER_USERNAME") != null) {
				String username = (String) request.getSession().getAttribute(
						"USER_USERNAME");
				FavouriesDAO dao = new FavouriesDAO();
				ArrayList<FavouriesMenuVMO> menuVMOList = dao
						.getMenuListByUser(username);
				request.setAttribute("MENUVMOLIST", menuVMOList);
				RequestDispatcher rd = request
						.getRequestDispatcher("favouries.jsp");
				rd.forward(request, response);
			} else {
				response.sendRedirect("home.jsp");
			}
		} else if (action.equals("removeMenu")) {
			String username = (String) request.getSession().getAttribute(
					"USER_USERNAME");
			int menuId = Integer.parseInt(request.getParameter("menuId"));
			FavouriesDAO dao = new FavouriesDAO();
			dao.delete(username, menuId);
			response.sendRedirect("UserServlet?action=loadFavouries");
		}
	}
}
